package com.example.service;

import com.example.dto.PageShowDTO;
import com.example.dto.PageTemplateDTO;
import com.example.entity.ComponentTemplate;
import com.example.entity.ListMapConverterJava8;
import com.example.entity.PageTemplate;
import com.example.entity.TemplateImage;
import com.example.mapper.ComponentTemplateInstMapper;
import com.example.mapper.PageTemplateMapper;
import com.example.mapper.TemplateImageMapper;
import com.example.service.PageTemplateService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.glass.ui.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageTemplateServiceImpl implements PageTemplateService {

    @Autowired
    private PageTemplateMapper pageTemplateMapper;

    @Autowired
    ComponentTemplateInstMapper componentTemplateInstMapper;

    @Autowired
    TemplateImageMapper templateImageMapper;

//    @Autowired
//    private ComponentTemplateInstMapper componentTemplateInstMapper;

    @Transactional
    @Override
    public Long createPageTemplate(PageTemplate pageTemplate) {

        return (long)pageTemplateMapper.insert(pageTemplate);
    }

    @Transactional
    @Override
    public Long saveComponentAndImage(PageTemplateDTO pageTemplateDTO) {
        PageTemplate pageTemplate = new PageTemplate();
        pageTemplate.setId(pageTemplateDTO.getPageTemplateId());
        pageTemplate.setImageBase64(pageTemplateDTO.getImageBase64());

        Map<String, Object> config = new HashMap<>();
        config.put("config", pageTemplateDTO.getComponentTemplateList());
        pageTemplate.setConfig(config);

        int id = pageTemplateMapper.updateById(pageTemplate);

        return (long)id;
    }

    @Override
    public boolean updatePageTemplate(PageTemplate pageTemplate) {
        return pageTemplateMapper.updateById(pageTemplate) > 0;
    }

    @Override
    public boolean deletePageTemplate(Long id) {
        return pageTemplateMapper.deleteById(id) > 0;
    }

    @Override
    public PageTemplate getPageTemplateById(Long id) {
        return pageTemplateMapper.selectById(id);
    }

    @Override
    public List<PageTemplate> getAllPageTemplates(String name) {
        return pageTemplateMapper.selectAll(name);
    }

    @Override
    public List<PageShowDTO> getAllPageInfo(String name) {

        List<PageTemplate> pageTemplate = pageTemplateMapper.selectAll(name);

        List<PageShowDTO> pageShowDTOS = new ArrayList<>();

        for (PageTemplate template : pageTemplate) {
            PageShowDTO pageShowDTO = new PageShowDTO();
            pageShowDTO.setPageId(template.getId());
            pageShowDTO.setName(template.getName());
            pageShowDTO.setRouting(template.getRouting());
//            pageShowDTO.setImageBase64(template.getImageBase64());
            pageShowDTO.setCreateTime(template.getCreateTime());
            pageShowDTO.setUpdateTime(template.getUpdateTime());
            pageShowDTO.setCreateBy(template.getCreateBy());
            pageShowDTO.setUpdateBy(template.getUpdateBy());
            pageShowDTOS.add(pageShowDTO);
        }
        return pageShowDTOS;
    }

    @Override
    public List<PageShowDTO> getAllPageInfoAndJsonData() {
        return null;
    }
}

